package com.CGFrame.struts.bean.pOperate;


import com.CGFrame.struts.POJOs.*;
import com.CGFrame.struts.bean.ProjectOperate;
import com.CGFrame.struts.bean.ProjectOperateFactory;

import java.util.*;
public class pOperateBean
{
	ProjectOperate pop = ProjectOperateFactory.getprojectOperate();
	public TreeNode TreeBuilder (Project project)
	{	
		TreeNode root = new  TreeNode();
		
		
		root.setNodeId(project.getProjectId()+"");
		root.setNodeName("Project  "+project.getProjectName());
		root.setNodeType("Project");
		root.setNodeLink("Type=Project&ID="+project.getProjectId());
		Vector vec = new Vector(); 
		for(Iterator it0 = project.getElementgroups().iterator();it0.hasNext();)
		{
			Elementgroup egp = (Elementgroup)it0.next();
			if(egp.getElementgroup()==null)
			{
				TreeNode egroup = new TreeNode();//��һ�㣺Ԫ�ؼ���
				egroup.setNodeId(egp.getElementGroupId()+"");
				egroup.setNodeName("ElementGroup  "+egp.getElementGroupName());
				egroup.setNodeType("Elementgroup");
				egroup.setNodeLink("Type=Elementgroup&ID="+egp.getElementGroupId());
				Vector vec1 = new Vector();
				for(Iterator it1 = egp.getElementgroups().iterator();it1.hasNext();)
				{
					Elementgroup egp1 = (Elementgroup)it1.next();
					TreeNode egroup1 = new TreeNode();//�ڶ���
					egroup1.setNodeId(egp1.getElementGroupId()+"");
					egroup1.setNodeName("ElementGroup��"+egp1.getElementGroupName());
					egroup1.setNodeType("Elementgroup");
					egroup1.setNodeLink("Type=Elementgroup&ID="+egp1.getElementGroupId());
					Vector vec2 = new Vector();
					for(Iterator it2 = egp1.getElementgroups().iterator();it2.hasNext();)
					{
						Elementgroup egp2 = (Elementgroup)it2.next();
						TreeNode egroup2 = new TreeNode();//�����
						egroup2.setNodeId(egp2.getElementGroupId()+"");
						egroup2.setNodeName("ElementGroup��"+egp2.getElementGroupName());
						egroup2.setNodeType("Elementgroup");
						egroup2.setNodeLink("Type=Elementgroup&ID="+egp2.getElementGroupId());
						Vector vec3 = new Vector();
						for(Iterator it3 = egp2.getElementgroups().iterator();it3.hasNext();)
						{
							Elementgroup egp3 = (Elementgroup)it3.next();
							TreeNode egroup3 = new TreeNode();//���Ĳ�
							egroup3.setNodeId(egp3.getElementGroupId()+"");
							egroup3.setNodeName("ElementGroup��"+egp3.getElementGroupName());
							egroup3.setNodeType("Elementgroup");
							egroup3.setNodeLink("Type=Elementgroup&ID="+egp3.getElementGroupId());
							Vector vec4 = new Vector();
							for(Iterator it4 = egp3.getElementgroups().iterator();it4.hasNext();)
							{
								Elementgroup egp4 = (Elementgroup)it4.next();
								TreeNode egroup4 = new TreeNode();//�����
								egroup4.setNodeId(egp4.getElementGroupId()+"");
								egroup4.setNodeName("ElementGroup��"+egp4.getElementGroupName());
								egroup4.setNodeType("Elementgroup");
								egroup4.setNodeLink("Type=Elementgroup&ID="+egp4.getElementGroupId());
								Vector vec5 = new Vector();
								for(Iterator it5 = egp4.getElements().iterator();it5.hasNext();)
								{
									Element e4 = (Element)it5.next();
									TreeNode ele4 = new TreeNode();
									ele4 = ElementTreeBuilder(e4);
									vec5.add(ele4);
								}
								egroup4.setChilds(vec5);
								vec4.add(egroup4);
							}
							for(Iterator it4 = egp3.getElements().iterator();it4.hasNext();)
							{
								Element e3 = (Element)it4.next();
								TreeNode ele3 = new TreeNode();
								ele3 = ElementTreeBuilder(e3);
								vec4.add(ele3);
							}
							egroup3.setChilds(vec4);
							vec3.add(egroup3);
						}	
						for(Iterator it3 = egp2.getElements().iterator();it3.hasNext();)
						{
							Element e2 = (Element)it3.next();
							TreeNode ele2 = new TreeNode();
							ele2 = ElementTreeBuilder(e2);
							vec3.add(ele2);
						}
						egroup2.setChilds(vec3);
						vec2.add(egroup2);
					}
					for(Iterator it2 = egp1.getElements().iterator();it2.hasNext();)
					{
						Element e1 = (Element)it2.next();
						TreeNode ele1 = new TreeNode();
						ele1 = ElementTreeBuilder(e1);
						vec2.add(ele1);
					}
					egroup1.setChilds(vec2);
					vec1.add(egroup1);
				}
				for(Iterator it1 = egp.getElements().iterator();it1.hasNext();)
				{
					Element e = (Element)it1.next();
					TreeNode ele = new TreeNode();
					ele = ElementTreeBuilder(e);
					vec1.add(ele);
				}
				egroup.setChilds(vec1);
				vec.add(egroup);
			}
			//egroup.setNodeLink(nodeLink);
			
		}
		for(Iterator it0 = project.getShotgroups().iterator();it0.hasNext();)
		{
			Shotgroup sgp = (Shotgroup)it0.next();
			if(sgp.getShotgroup()==null)
			{
				TreeNode sgroup = new TreeNode();//��һ�㣺��ͷ����
				sgroup.setNodeId(sgp.getShotGroupId()+"");
				sgroup.setNodeName("ShotGroup��"+sgp.getShotGroupName());
				sgroup.setNodeType("Shotgroup");
				sgroup.setNodeLink("Type=Shotgroup&ID="+sgp.getShotGroupId());
				Vector vec1 = new Vector();
				for(Iterator it1 = sgp.getShotgroups().iterator();it1.hasNext();)
				{
					Shotgroup sgp1 = (Shotgroup)it1.next();
					TreeNode sgroup1 = new TreeNode();//�ڶ���
					sgroup1.setNodeId(sgp1.getShotGroupId()+"");
					sgroup1.setNodeName("ShotGroup��"+sgp1.getShotGroupName());
					sgroup1.setNodeType("Shotgroup");
					sgroup1.setNodeLink("Type=Shotgroup&ID="+sgp1.getShotGroupId());
					Vector vec2 = new Vector();
					for(Iterator it2 = sgp1.getShotgroups().iterator();it2.hasNext();)
					{
						Shotgroup sgp2 = (Shotgroup)it2.next();
						TreeNode sgroup2 = new TreeNode();//�����
						sgroup2.setNodeId(sgp2.getShotGroupId()+"");
						sgroup2.setNodeName("ShotGroup��"+sgp2.getShotGroupName());
						sgroup2.setNodeType("Shotgroup");
						sgroup2.setNodeLink("Type=Shotgroup&ID="+sgp2.getShotGroupId());
						Vector vec3 = new Vector();
						for(Iterator it3 = sgp2.getShotgroups().iterator();it3.hasNext();)
						{
							Shotgroup sgp3 = (Shotgroup)it3.next();
							TreeNode sgroup3 = new TreeNode();//���Ĳ�
							sgroup3.setNodeId(sgp3.getShotGroupId()+"");
							sgroup3.setNodeName("ShotGroup��"+sgp3.getShotGroupName());
							sgroup3.setNodeType("Shotgroup");
							sgroup3.setNodeLink("Type=Shotgroup&ID="+sgp3.getShotGroupId());
							Vector vec4 = new Vector();
							for(Iterator it4 = sgp3.getShotgroups().iterator();it4.hasNext();)
							{
								Shotgroup sgp4 = (Shotgroup)it4.next();
								TreeNode sgroup4 = new TreeNode();//�����
								sgroup4.setNodeId(sgp4.getShotGroupId()+"");
								sgroup4.setNodeName("ShotGroup��"+sgp4.getShotGroupName());
								sgroup4.setNodeType("Shotgroup");
								sgroup4.setNodeLink("Type=Shotgroup&ID="+sgp4.getShotGroupId());
								Vector vec5 = new Vector();
								for(Iterator it5 = sgp4.getShots().iterator();it5.hasNext();)
								{
									Shot s4 = (Shot)it5.next();
									TreeNode sh4 = new TreeNode();
									sh4 = ShotTreeBuilder(s4);
									vec5.add(sh4);
								}
								sgroup4.setChilds(vec5);
								vec4.add(sgroup4);
							}
							for(Iterator it4 = sgp3.getShots().iterator();it4.hasNext();)
							{
								Shot s3 = (Shot)it4.next();
								TreeNode sh3 = new TreeNode();
								sh3 = ShotTreeBuilder(s3);
								vec4.add(sh3);
							}
							sgroup3.setChilds(vec4);
							vec3.add(sgroup3);
						}	
						for(Iterator it3 = sgp2.getShots().iterator();it3.hasNext();)
						{
							
							Shot s2 = (Shot)it3.next();
							TreeNode sh2 = new TreeNode();
							sh2 = ShotTreeBuilder(s2);
							vec3.add(sh2);
						}
						sgroup2.setChilds(vec3);
						vec2.add(sgroup2);
					}
					for(Iterator it2 = sgp1.getShots().iterator();it2.hasNext();)
					{
						Shot s1 = (Shot)it2.next();
						TreeNode sh1 = new TreeNode();
						sh1 = ShotTreeBuilder(s1);
						vec2.add(sh1);
					}
					sgroup1.setChilds(vec2);
					vec1.add(sgroup1);
				}
				for(Iterator it1 = sgp.getShots().iterator();it1.hasNext();)
				{
					Shot s = (Shot)it1.next();
					TreeNode sh = new TreeNode();
					sh = ShotTreeBuilder(s);
					vec1.add(sh);
				}
				//egroup.setNodeLink(nodeLink);
				sgroup.setChilds(vec1);
				vec.add(sgroup);
			}
			
		}
		root.setChilds(vec);
		return root;
	}
	
	public TreeNode ElementTreeBuilder (Element element)
	{
		TreeNode root = new TreeNode();
		if(element.getPlanEndTime()==null||element.getPlanStartTime()==null)
		{
			root.setNodeFlag("1");
		}
		root.setNodeName("Element��"+element.getElementName());
		root.setNodeId(element.getElementId()+"");
		root.setNodeType("Element");
		root.setNodeLink("Type=Element&ID="+element.getElementId());
		Vector vec = new Vector();
		for(Iterator it = element.getMakes().iterator();it.hasNext();)
		{
			Make mk = (Make)it.next();
			TreeNode mknode = new TreeNode();
			mknode.setNodeId(mk.getMakeId()+"");
			mknode.setNodeName("Make��"+mk.getMakeName());
			mknode.setNodeType("Make");
			mknode.setNodeLink("Type=Make&ID="+mk.getMakeId());
			Vector vec1 = new Vector();
			for(Iterator it1 = mk.getTasks().iterator();it1.hasNext();)
			{
				Task tsk = (Task)it1.next();
				TreeNode tsknode = new TreeNode();
				tsknode.setNodeName(tsk.getTaskName());
				tsknode.setNodeId("Task��"+tsk.getTaskId()+"");
				tsknode.setNodeType("Task");
				tsknode.setNodeLink("Type=Task&ID="+tsk.getTaskId());
				vec1.add(tsknode);
			}
			mknode.setChilds(vec1);
			vec.add(mknode);
		}
		root.setChilds(vec);
		return root;
	}
	
	public TreeNode ShotTreeBuilder (Shot shot)
	{
		TreeNode root = new TreeNode();
		root.setNodeName("Shot��"+shot.getShotName());
		if(shot.getPlanEndTime()==null||shot.getPlanStartTime()==null)
		{
			root.setNodeFlag("1");
		}
		root.setNodeId(shot.getShotId()+"");
		root.setNodeType("Shot");
		root.setNodeLink("Type=Shot&ID="+shot.getShotId());
		Vector vec = new Vector();
		for(Iterator it = shot.getMakes().iterator();it.hasNext();)
		{
			Make mk = (Make)it.next();
			TreeNode mknode = new TreeNode();
			mknode.setNodeId(mk.getMakeId()+"");
			mknode.setNodeName("Make��"+mk.getMakeName());
			mknode.setNodeType("Make");
			mknode.setNodeLink("Type=Make&ID="+mk.getMakeId());
			Vector vec1 = new Vector();
			for(Iterator it1 = mk.getTasks().iterator();it1.hasNext();)
			{
				Task tsk = (Task)it1.next();
				TreeNode tsknode = new TreeNode();
				tsknode.setNodeName(tsk.getTaskName());
				tsknode.setNodeId("Task��"+tsk.getTaskId()+"");
				tsknode.setNodeLink("Type=Task&ID="+tsk.getTaskId());
				tsknode.setNodeType("Task");
				vec1.add(tsknode);
			}
			mknode.setChilds(vec1);
			vec.add(mknode);
		}
		root.setChilds(vec);
		return root;
	}
	
	public void PrintTree(TreeNode tree)
	{
		Vector V = tree.getChilds();
		for(int i=0;i<V.size();i++)
		{
			TreeNode t1 = (TreeNode)V.elementAt(i);
			System.out.println("1  "+t1.getNodeType()+" "+t1.getNodeName());
			Vector V1 = t1.getChilds();
			for(int j=0;j<V1.size();j++)
			{
				TreeNode t2 = (TreeNode)V1.elementAt(j);
				System.out.println("2    "+t2.getNodeType()+" "+t2.getNodeName());
				Vector V2 = t2.getChilds();
				for(int k=0;k<V2.size();k++)
				{
					TreeNode t3 = (TreeNode)V2.elementAt(k);
					System.out.println("3      "+t3.getNodeType()+" "+t3.getNodeName());
					Vector V3 = t3.getChilds();
					for(int l=0;l<V3.size();l++)
					{
						TreeNode t4 = (TreeNode)V3.elementAt(l);
						System.out.println("4        "+t4.getNodeType()+" "+t4.getNodeName());
						Vector V4 = t4.getChilds();
						if(V4!=null)
						{
							for(int m=0;m<V4.size();m++)
							{
								TreeNode t5 = (TreeNode)V4.elementAt(m);
								System.out.println("5          "+t5.getNodeType()+" "+t5.getNodeName());
								Vector V5 = t5.getChilds();
								if(V5!=null)
								{
									for(int n=0;n<V5.size();n++)
									{
										TreeNode t6 = (TreeNode)V5.elementAt(n);
										System.out.println("6          "+t6.getNodeType()+" "+t6.getNodeName());
										Vector V6 = t6.getChilds();
										if(V6!=null)
										{
											for(int o=0;o<V6.size();o++)
											{
												TreeNode t7 = (TreeNode)V6.elementAt(n);
												System.out.println("7          "+t7.getNodeType()+" "+t7.getNodeName());
												Vector V7 = t7.getChilds();
												if(V7!=null)
												{
													for(int p=0;p<V6.size();p++)
													{
														TreeNode t8 = (TreeNode)V6.elementAt(n);
														System.out.println("8          "+t8.getNodeType()+" "+t8.getNodeName());
													}
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}

	public void DeleteTasks(String type ,Object object )
	{
		if(type.equals("Elementgroup"))
		{
			Elementgroup egp = (Elementgroup)object;
			HashSet hash = new HashSet();
			hash = getAllElements(egp,hash);
			for(Iterator it = hash.iterator();it.hasNext();)
			{
				Element element = (Element)it.next();
				DeleteTasks("Element",element);
			}	
		}
		else if(type.equals("Shotgroup"))
		{
			Shotgroup sgp = (Shotgroup)object;
			HashSet hash = new HashSet();
			hash = getAllShots(sgp,hash);
			for(Iterator it = hash.iterator();it.hasNext();)
			{
				Shot shot = (Shot)it.next();
				DeleteTasks("Shot",shot);
			}
		}
		else if(type.equals("Element"))
		{
			Element element = (Element)object;
			for(Iterator it = element.getMakes().iterator();it.hasNext();)
			{
				Make make = (Make)it.next();
				DeleteTasks("Make",make);
			}
		}
		else if(type.equals("Shot"))
		{
			Shot shot = (Shot)object;
			for(Iterator it = shot.getMakes().iterator();it.hasNext();)
			{
				Make make = (Make)it.next();
				DeleteTasks("Make",make);
			}
		}
		else if(type.equals("Make"))
		{
			Make make = (Make)object;

			Status status = (Status)pop.UniqueQuery("Status", "statusId,100");
			for(Iterator it = make.getTasks().iterator();it.hasNext();)
			{
				Task task = (Task)it.next();
				
				task.setStatus(status);
				//temp_task.setTaskrelationsForTask1id(task.getTaskrelationsForTask1id());
				//temp_task.setTaskrelationsForTask2id(task.getTaskrelationsForTask2id());
				for(Iterator it1 = task.getTaskrelationsForTask1id().iterator();it1.hasNext();)
				{
					Taskrelation tsk_rela = (Taskrelation)it1.next();
					pop.delete(tsk_rela);
				}
				for(Iterator it1 = task.getTaskrelationsForTask2id().iterator();it1.hasNext();)
				{
					Taskrelation tsk_rela = (Taskrelation)it1.next();
					pop.delete(tsk_rela);
				}
				pop.saveObject(task);
			}
		}
	}
	
	public HashSet getGroupElements (HashSet hash,Elementgroup egp)
	{
		for(Iterator it = egp.getElements().iterator();it.hasNext();)
		{
			Element element = (Element)it.next();
			hash.add(element);
		}
		return hash;
	}
	public HashSet getAllElements(Elementgroup egp,HashSet hash)
	{
		hash = getGroupElements(hash,egp);
		for(Iterator it = egp.getElementgroups().iterator();it.hasNext();)
		{
			Elementgroup egp1 = (Elementgroup)it.next();
			hash = getAllElements(egp1,hash);
		}
		return hash;
	}
	public HashSet getGroupShots (HashSet hash,Shotgroup sgp)
	{
		for(Iterator it = sgp.getShots().iterator();it.hasNext();)
		{
			Shot shot = (Shot)it.next();
			hash.add(shot);
		}
		return hash;
	}
	public HashSet getAllShots(Shotgroup sgp,HashSet hash)
	{
		hash = getGroupShots(hash,sgp);
		for(Iterator it = sgp.getShotgroups().iterator();it.hasNext();)
		{
			Shotgroup sgp1 = (Shotgroup)it.next();
			hash = getAllShots(sgp1,hash);
		}
		return hash;
	}

	public void DeleteUnit (String type,String id)
	{
		Status status = (Status)pop.UniqueQuery("Status", "statusId,100");
		if(type.equals("Elementgroup"))
		{
			Elementgroup egp = (Elementgroup)pop.UniqueQuery("Elementgroup", "elementGroupId,"+id);
			DeleteTasks("Elementgroup",egp);
		//	Elementgroup temp_egp = new Elementgroup();
			egp.setStatus(status);
	//		System.out.println("Update Elementgroup");
			pop.saveObject(egp);
		}
		else if(type.equals("Shotgroup"))
		{
			Shotgroup sgp = (Shotgroup)pop.UniqueQuery("Shotgroup", "shotGroupId,"+id);
			DeleteTasks("Shotgroup",sgp);
	//		Shotgroup temp_sgp = new Shotgroup();
			sgp.setStatus(status);
			pop.saveObject(sgp);
		}
		else if(type.equals("Shot"))
		{
			Shot shot = (Shot)pop.UniqueQuery("Shot", "shotId,"+id);
			DeleteTasks("Shot",shot);
	//		Shot temp_shot = new Shot();
			//temp_shot.setMakes(shot.getMakes());
			shot.setStatus(status);
			pop.saveObject(shot);
		}
		else if (type.equals("Element"))
		{
			Element elem = (Element)pop.UniqueQuery("Element", "elementId,"+id);
			DeleteTasks("Element",elem);
	//		Element temp_elem = new Element();
			elem.setStatus(status);
			pop.saveObject(elem);
		}
		else if(type.equals("Make"))
		{
			Make make = (Make)pop.UniqueQuery("Make", "makeId,"+id);
			DeleteTasks("Make",make);
	//		Make temp_make = new Make();
			make.setStatus(status);
			//temp_make.setTasks(make.getTasks());
			for(Iterator it = make.getMakerelationsForMake1id().iterator();it.hasNext();)
			{
				Makerelation mk_rela = (Makerelation)it.next();
				pop.delete(mk_rela);
			}
			for(Iterator it = make.getMakerelationsForMake2id().iterator();it.hasNext();)
			{
				Makerelation mk_rela = (Makerelation)it.next();
				pop.delete(mk_rela);
			}
			pop.saveObject(make);
		}
		else if(type.equals("Task"))
		{
			Task task = (Task)pop.UniqueQuery("Task", "taskId,"+id);
		//	Task temp_Task = new Task();
			task.setStatus(status);
			for(Iterator it = task.getTaskrelationsForTask1id().iterator();it.hasNext();)
			{
				Taskrelation tsk_rela = (Taskrelation)it.next();
				pop.delete(tsk_rela);
			}
			for(Iterator it = task.getTaskrelationsForTask2id().iterator();it.hasNext();)
			{
				Taskrelation tsk_rela = (Taskrelation)it.next();
				pop.delete(tsk_rela);
			}
			pop.saveObject(task);
		}
	}

}